package cn.zwx.mvcframework.webmvc.servlet;

import java.io.File;

/**
 * @author zhangwenxue
 */
public class ZwxViewResolver {

    /**
     * 默认匹配后缀
     */
    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";

    /**
     * 静态模板的目录
     */
    private File templateRootDir;

    public ZwxViewResolver(String templateRoot) {
        // 转为文件对象进行保存
        this.templateRootDir =  new File(templateRoot);
    }

    /**
     * 拼接对应的View对象
     * @param viewName 返回对应的viewName
     * @return ZwxView
     */
    public ZwxView resolveViewName(String viewName){
        if ("".equals(viewName.trim())){
            return null;
        }
       String suffixViewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX)?viewName:viewName+DEFAULT_TEMPLATE_SUFFIX;
       File file = new File((templateRootDir.getPath() + "/" + suffixViewName).replaceAll("/+", "/"));
       return new ZwxView(file);
    }
}
